<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 文章管理
 *
 * @icon fa fa-circle-o
 */
class Article extends Backend
{
    
    /**
     * Article模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Article');

        $tree = Tree::instance();
        $tree->init(collection(model('Category')->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
        foreach ($this->categorylist as $k => $v)
        {
            $categorydata[$v['id']] = $v;
        }
        $this->view->assign("categoryList", $categorydata);

    }

    /**
     * 查看
     */
    public function index()
    {
        $this->relationSearch = true;
        $this->searchFields = "category.id,id";
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with("category")
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with("category")
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function getStatusList()
    {
        $list = $this->model->statusList;
        $searchlist = [];
        foreach ($list as $key => $value)
        {
            $searchlist[] = ['id' => $key, 'name' => $value];
        }
        $data = ['searchlist' => $searchlist];
        $this->success('', null, $data);
    }

    public function categorySearchList($pid = 0)
    {
        $Category = model('Category');
        $result   = $Category->where('pid', $pid)->select();
        $searchlist = [];
        foreach ($result as $key => $value) {
            $searchlist[] = ['value' => $value['id'], 'name' => $value['name']];
        }
        $this->success('', null, $searchlist);
    }



}
